$(function() {
	
    function h() {
        $(".img-container").hasClass("edit") && b.cropper("setData", l)
    }
    $("#myTab a").click(function(a) {
        a.preventDefault();
        $(this).tab("show")
    });
    $("#settings div.btn-default").on("click", function() {
        var a = $(".v-site-name").val(),
            c = $(".v-site-pass").val();
        $.ajax({
            type: "POST",
            url: "actions.php",
            data: "site_name=" + a + "&site_pass=" + c + "&action=changeSiteInfo",
            cache: !1,
            success: function(a) {
                alert(a);
                window.location.href = "?tab=settings";
                $(document).ready(function(a) {
                    $(".nav-tabs li:last").click()
                })
            }
        })
    });
   
    
    //注销登录
    $(".log-out").on("click", function() {
        $.ajax({
            type: "POST",
            url: "actions.php",
            data: "action=logout",
            cache: !1,
            success: function() {
                history.go(0)
            }
        })
    });
    //删除
    $(".manage-user").on("click", ".user-delete", function() {
		var $tr = $(this).parents('tr');
        var mymessage = confirm("确定要删除吗?");
        if (mymessage === true) {
			var id = $tr.find('input:hidden').val();
			$.ajax({
				 type: "POST",
				 url: URL+"/deleteGroup",
				 data: {"id":id},
				 success: function(data){
							 $tr.remove();
						  }
			});
        }
    });
    /*---------------------组别管理---------------------*/
    // 编辑
    $(".manage-user").on("click", ".user-edit", function() {
        var $modal = $('#myModa0');
        $modal.attr('data-type', 'editor');
        // 获取数据
        var $tr = $(this).parents('tr');
        $(this).parents('tr').addClass('editor');
        var name1 = $tr.find('td').eq(1).text();
        var name2 = $tr.find('td').eq(2).text();
		var id = $tr.find('input:hidden').val();
        $modal.find('.name1').val(name1);
        $modal.find('.name2').val(name2);
		$modal.find('.id').val(id);
    });
    //新增
    $('#btn-group-add').on("click", function() {
        var $modal = $('#myModa0');
        $modal.attr('data-type', 'add');
        $modal.find(".name1").val('');
        $modal.find(".name2").val('');
    });
    //模态框确定
    $("#addzubie").on("click", function() {
        // var $(this).parents('.modal')
        var $modal = $('#myModa0');
        var name1 = $(".name1").val();
        var name2 = $(".name2").val();
        // console.log($(this).parents('.modal').data('type'));
        var modalType = $(this).parents('.modal').attr('data-type');
        if (modalType === "editor") {
			var id = $(".id").val();
            var $tr = $('.manage-user').find('tr.editor');
			$.ajax({
				 type: "POST",
				 url: URL+"/updateGroup",
				 data: {"id":id, "name":name1, "ename":name2,"type":"update"},
				 success: function(data){
							$tr.find('td').eq(1).text(name1);
							$tr.find('td').eq(2).text(name2);
							$tr.removeClass('editor');
							$modal.find('.close').trigger('click');
						  }
			});
            
        } else if (modalType === "add") {
			 $.ajax({
				 type: "POST",
				 url: URL+"/addGroup",
				 data: {name:name1, ename:name2},
				 success: function(data){
							var newzubie = "";
							newzubie = '<tr><td>'+data.orderid+'</td><td>' + name1 + '</td><td>' + name2 + '</td><td><input type="hidden" name="id" value="'+data.id+'"><a class="label label-primary user-edit" data-toggle="modal" data-target="#myModa0"> 编辑</a>  <a class="label label-primary user-delete">删除</a>  <a class="label label-danger user-choose">选择人员</a></td></tr>';
							$('#home table').append(newzubie);
							$modal.find('.close').trigger('click');
						  }
			});
            
        }
    });
 
    $("#buttonUpload").click(function() {
        b.data("cropper");
        $.ajaxFileUpload({
            url: "upd.php",
            secureuri: !1,
            fileElementId: "fileToUpload",
            dataType: "json",
            success: function(a, c) {
                "undefined" !=
                typeof a.error && ("" != a.error ? alert(a.error) : b.cropper("setImgSrc", a.msg))
            }, 
            error: function(a, c, f) {
                alert(f)
            }
        });
        return !1
    });
    $("#set-img-src").click(function() {
        b.data("cropper");
        var a = $("#set-img-src-input").val();
        b.cropper("setImgSrc", a)
    });

    var b = $(".img-container img"),
        d = $(".img-container");
    $("#data-x1");
    $("#data-y1");
    $("#data-x2");
    $("#data-y2");
    $("#data-height");
    $("#data-width");
    b.cropper({
        aspectRatio: .8,
        preview: ".img-preview",
        done: function(a) {
            d.attr("x1", a.x1);
            d.attr("y1", a.y1);
            d.attr("h", a.height);
            d.attr("w", a.width)
        }
    });
    var m = $("#drag-move");
    b.on("dragmove", function() {
        m.addClass("btn-info").siblings().removeClass("btn-info")
    });
    var l = {
        x1: 0,
        y1: 0,
        width: 208,
        height: 260
    };
    $("#enable").click(function() {
        b.cropper("enable")
    });
    var k = new Image,
        n = $(".img-container img").attr("src");
    k.onload = function() {
        h()
    };
    //k.src = n;
    $(".reset").on("click", function() {
        h()
    });
    $(".search").click(function() {
        $("#profile .manage-user tbody").show();
        $("#profile .manage-user tr").hide().filter(":contains('" + $(this).val() + "')").show()
    });
    
});
